/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.googlecode.mindlistening.freemind;

import com.googlecode.mindlistening.DocumentStorage;
import com.googlecode.mindlistening.PropertiesStorage;
import com.googlecode.mindlistening.converter.FreemindXMLConverter;
import com.googlecode.mindlistening.domain.Conversation;
import freemind.modes.MindMapNode;
import freemind.modes.attributes.Attribute;
import java.io.File;
import java.io.FileInputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author ryan
 */
public class FreeMindNodeDocumentStorage implements DocumentStorage {

    SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy");
    MindMapNode node;
    PropertiesStorage storage;
    FreemindXMLConverter converter = new FreemindXMLConverter();


	public FreeMindNodeDocumentStorage(MindMapNode node, PropertiesStorage storage) {
		this.node = node;
		this.storage = storage;
		node.createAttributeTableModel();
	}
	public void setStartDate(Date date) {
		Attribute att = new Attribute("startDate", formatter.format(date));
		node.getAttributes().addRowNoUndo(att);
	}
	public Date getStartDate() {
		String start  = node.getAttribute("startDate");
                try {
                    return formatter.parse(start);
                } catch (Exception ex) {}
                return null;
		
	}
	public void setEndDate(Date date) {
		Attribute att = new Attribute("endDate", formatter.format(date));
		node.getAttributes().addRowNoUndo(att);
	}

	public Date getEndDate(){
		String end  = node.getAttribute("endDate");
                try {
                    return formatter.parse(end);
                } catch (Exception ex) {}
                return null;
	}
	public File getFileLocation(){
		return node.getMap().getFile();
	}


	public boolean saveCurrentDocument(){
		return node.getMap().getModeController().save();
	}

	public void setUploadURL(String url){
		Attribute att = new Attribute("uploadURL", url);
		node.getAttributes().addRowNoUndo(att);
	}
	public String getUploadURL(){
		return node.getAttribute("uploadURL");
	}
	public Conversation convertToConversation() {
            try {
                FileInputStream fis = new FileInputStream(getFileLocation());
                try {
                        converter.setStartTagOffset(Integer.parseInt(storage.loadProperty("tagStartOffset")));
                        converter.setDefaultTagDuration(Integer.parseInt(storage.loadProperty("tagDuration")));
                } catch (Exception e) {}
                return converter.parseConversation(fis);
            } catch (Exception e) {}
            return null;
        }


}
